<script setup lang="ts">
import logoUrl from '@/assets/logo.png'
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRoute } from 'vue-router'
import {useAuthStore} from '@/stores/authStore'

const links = [
  { title: '文档', url: 'https://element-plus.org/zh-CN/' },
  { title: '博客', url: 'https://songbaicheng.github.io/' },
  { title: '社区', url: 'https://gitee.com/songbaicheng/12306-web' }
]
const username = ref('')
const route = useRoute()
const authStore = useAuthStore()

onMounted(() => {
  const storedUsername = authStore.getUsername
  if (storedUsername) {
    username.value = storedUsername
  }
})

const logout = () => {
  authStore.logout()
  location.href = 'login'
  ElMessage('退出成功')
}

const toLogin = () => {
  location.href = 'login'
}
</script>

<template>
  <div class="header-wrapper">
    <el-row type="flex" justify="space-between" align="middle">
      <el-col :span="2">
        <el-image class="logo" :src="logoUrl" fit="fill" />
      </el-col>
      <el-col :span="9">
        <el-row type="flex" justify="space-between" align="middle">
          <el-col v-for="(link, index) in links" :key="index" :span="24 / links.length">
            <a :href="link.url" target="_blank">{{ link.title }}</a>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="3">
        <div v-if="username">
          <el-dropdown>
            <span>{{ username }}</span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <div v-else-if="route.fullPath == '/login'">
          <span>欢迎登录！</span>
        </div>
        <div v-else>
          <span @click="toLogin">去登录</span>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
@import '@/assets/variables.scss';

.header-wrapper {
  height: 64px;
  margin: $common-margin;
  background-color: $theme-color;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.9);
}

el-row {
  height: 100%;
  align-items: center;
}

.logo {
  height: 40px;
  cursor: pointer;
}
</style>